from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404, Http404
from django.template import Template, RequestContext, Context, loader

from content.models import *

def home(request):
    if settings.HOMEPAGE_POST_CATEGORY_RESTRICTION:
        news_items = Post.objects.filter(category__name__in=settings.HOMEPAGE_POST_CATEGORY_RESTRICTION)[:4]
    else:
        news_items = Post.objects.all()
    return render_to_response('content/homepage.html', locals(), context_instance=RequestContext(request))
    
def artist_home(request, artist_slug):
    artist = get_object_or_404(Artist,slug=artist_slug)
    return render_to_response('content/artist-home.html', locals(), context_instance=RequestContext(request))
    
def biography(request, person_slug):
    person = get_object_or_404(Person,slug=person_slug)
    stints = BandMembership.objects.filter(member=person)
    return render_to_response('content/biography.html', locals(), context_instance=RequestContext(request))
    
def discography(request, artist_slug):
    artist = get_object_or_404(Artist,slug=artist_slug)
    return render_to_response('content/discography.html', locals(), context_instance=RequestContext(request))
        
def song_text(rquest, artist_slug, album_slug, song_slug):
    artist = get_object_or_404(Artist,slug=artist_slug)
    album = get_object_or_404(Album, slug=album_slug, artist=artist)
    song = get_object_or_404(Track, slug=song_slug, album=album)
    return HttpResponse(song.text)
    
def lyrics_overview(request, artist_slug):
    from django.db.models import Q
    artist = get_object_or_404(Artist,slug=artist_slug)
    qset = Q(album__artist=artist) & ( Q(lyrics__gt='') | Q(lyric_image__gt='') )
    lyric_songs = Track.objects.filter(qset)
    return render_to_response('content/lyrics_overview.html', locals(), context_instance=RequestContext(request))
    
def album_page(request, artist_slug, album_slug):
    artist = get_object_or_404(Artist,slug=artist_slug)
    album = get_object_or_404(Album, slug=album_slug, artist=artist)
    return render_to_response('content/album.html', locals(), context_instance=RequestContext(request))
    
def lyrics_page(request, artist_slug, song_slug):
    artist = get_object_or_404(Artist,slug=artist_slug)
    song = get_object_or_404(Track, slug=song_slug)
    album = song.album
    return render_to_response('content/lyrics.html', locals(), context_instance=RequestContext(request))
    
def blog_post(request, year, month, post_slug):
    date_lower = datetime.datetime(year=int(year), month=int(month), day=1)
    date_upper = datetime.datetime(year=int(year), month=int(month), day=31)
    post = get_object_or_404(Post, date__gte=date_lower, date__lte=date_upper, slug=post_slug)
    return render_to_response('content/blog_page.html', locals(), context_instance=RequestContext(request))
    
def blog_archive(request):
    posts_by_category = {}
    categories = PostCategory.objects.all()
    for c in categories:
        posts = list(Post.objects.filter(category=c).order_by('-date'))
        posts_by_category.update({c: posts})
                                                         
    print posts_by_category
    return render_to_response('content/blog_archive.html', locals(), context_instance=RequestContext(request))
    

    
